package com.java.demo.data;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @auther zhangwj
 * @date 2020/8/17 4:33 下午
 */
public class CacheDemo {

    public static void main(String[] args) {
        Cache<String, String> cache = new Cache<>();

        System.out.println(1 << 16);
    }
}

class optionMap<K, V> implements Runnable {

    private Cache<K, V> cache;

    optionMap(Cache<K, V> cache) {
        this.cache = cache;
    }

    @Override
    public void run() {
        //cache.map.get()
    }
}


class Cache<K, V> {
    final Map<K, V> map = new HashMap<>();

    final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();

    final Lock rLock = readWriteLock.readLock();

    final Lock wLock = readWriteLock.writeLock();

    V get(K key) {
        rLock.lock();
        try {
            return map.get(key);
        } finally {
            rLock.unlock();
        }
    }

    V put(K key, V value) {
        wLock.lock();
        try {
            return map.put(key, value);
        } finally {
            wLock.unlock();
        }
    }
}